Const WINDOW_HANDLE = 0
Const BIF_EDITBOX = &H10
Const BIF_NONEWFOLDER = &H0200
Const BIF_RETURNONLYFSDIRS = &H1
Const ForReading = 1, ForWriting = 2, TristateTrue = -1

Set objShell = CreateObject("Shell.Application")
Set wshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

'**Browse For Folder To Be Processed
strPrompt = "Please select the folder to process."
intOptions = BIF_RETURNONLYFSDIRS + BIF_NONEWFOLDER + BIF_EDITBOX
strTargetPath = wshShell.SpecialFolders(".")
objStartFolder = Browse4Folder(strPrompt, intOptions, strTargetPath)

'Duyet tat ca files va thu muc con trong thu muc objStartFolder
'Set objFolder = objFSO.GetFolder(objStartFolder)
'ShowSubfolders objFSO.GetFolder(objStartFolder)

'Tao list file
Set objNewFile = objFSO.CreateTextFile(objStartFolder & "\filmlist.csv", True)
Set parentFolder = objFSO.GetFolder(objStartFolder)
Dim str
Wscript.Echo parentFolder
For Each Subfolder in parentFolder.SubFolders
	Set a = objFSO.GetFolder(Subfolder.Path)
	For Each Subfolder1 in  a.SubFolders
		Wscript.Echo Subfolder1.Name
		str = Subfolder1.Name
		objNewFile.WriteLine(strClean(str))
	Next
 Next

objNewFile.Close

Sub ShowSubFolders(Folder)
	Set fso = CreateObject("Scripting.FileSystemObject")
    For Each Subfolder in Folder.SubFolders
		Wscript.Echo Subfolder.Path
        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
			sExtension = fso.GetExtensionName(objFile.Name)
			If sExtension = "srt" Then		
				Wscript.Echo objFile.Name
				'Mo file			
				Set stream =fso.OpenTextFile(Subfolder.Path & "\" & objFile.Name,ForReading,False,TristateTrue)
				strg = stream.ReadAll()
				stream.Close
				Set stream = Nothing
				
				'Thay text
				Set re = New RegExp
				re.Global = True
				re.IgnoreCase = False
				re.Multiline = True
				re.Pattern = "khohd"
				strg = re.Replace(strg,"hdchonloc")
				
				'Ghi file
				Set stream =fso.OpenTextFile(Subfolder.Path & "\" & objFile.Name,ForWriting,True,TristateTrue)
				stream.Write(strg)
				stream.Close
				Set stream = Nothing 	
			End If
					
        Next
        Wscript.Echo
        ShowSubFolders Subfolder
    Next
End Sub

'**Browse4Folder Function
Function Browse4Folder(strPrompt, intOptions, strRoot)
    Dim objFolder, objFolderItem

    On Error Resume Next

    Set objFolder = objShell.BrowseForFolder(0, strPrompt, intOptions, strRoot)
    If (objFolder Is Nothing) Then
      Wscript.Quit
    End If
    Set objFolderItem = objFolder.Self
    Browse4Folder = objFolderItem.Path
    Set objFolderItem = Nothing
    Set objFolder = Nothing
End Function

Function strClean (strtoclean)
Dim objRegExp, outputStr
Set objRegExp = New Regexp

objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = "((?![a-zA-Z0-9]).)+"
outputStr = objRegExp.Replace(strtoclean, "-")

objRegExp.Pattern = "\-+"
outputStr = objRegExp.Replace(outputStr, "-")

strClean = outputStr
End Function